﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;
using negocios;
using publico;
using objetos;

namespace softsales.pagina.menu
{
    public partial class menu_formulario : System.Web.UI.Page
    {
        OBJ_TB_MENU o_tb_menu = new OBJ_TB_MENU();
        BLL_TB_MENU b_tb_menu = new BLL_TB_MENU();

        OBJ_TB_MENU_USUARIO o_tb_menu_usuario = new OBJ_TB_MENU_USUARIO();
        BLL_TB_MENU_USUARIO b_tb_menu_usuario = new BLL_TB_MENU_USUARIO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (PUB_SESSAO.Logado())
            {

                if (!IsPostBack)
                {
                    LBL_TITULO_PAGINA.Text = "Gerenciar Menu";
                    PreencheControles();
                }
                    

            }

        }
           
        private void PreencheControles()
        {
            b_tb_menu.ListaMenu(DDL_ID_MENU_PAI, true);
            b_tb_menu.ListaMenu(LST_MENU, false);         
        }

        protected void BTN_SALVAR_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;

            try
            {
                if (TXT_DESCRICAO.Text != string.Empty && TXT_MENU.Text != string.Empty && TXT_URL.Text != string.Empty)
                {
                    int id_menu_pai = 0;
                    if (DDL_ID_MENU_PAI.SelectedValue != string.Empty)
                    {
                        id_menu_pai = Convert.ToInt32(DDL_ID_MENU_PAI.SelectedValue);
                    }


                    o_tb_menu.id_menu_pai = id_menu_pai;
                    o_tb_menu.menu = TXT_MENU.Text;
                    o_tb_menu.ordem = Convert.ToInt32(TXT_ORDEM.Text);
                    o_tb_menu.descricao = TXT_DESCRICAO.Text;
                    o_tb_menu.parametro = TXT_PARAMETRO.Text;
                    o_tb_menu.sit_ativo = CHK_SIT_ATIVO.Checked;
                    o_tb_menu.url = TXT_URL.Text;

                    if (HF_ID.Value != string.Empty)
                    {
                        o_tb_menu.id = Convert.ToInt32(HF_ID.Value);

                        b_tb_menu.Alterar(o_tb_menu);
                        msg = PUB_MENSAGEM.MSG_ALTERACAO;

                    }
                    else
                    {
                        b_tb_menu.Incluir(o_tb_menu);
                         msg = PUB_MENSAGEM.MSG_INCLUSAO;
                    }
                 
                    Master.MSG_ALERTA(msg, "default.aspx", false);
                    
                    PreencheControles();

                }

            }
            catch (Exception)
            {
                
                throw;
            }
         
            
            
        }

        protected void BTN_CANCELAR_Click(object sender, EventArgs e)
        {
       
            Response.Redirect("~/default.aspx", false);
        }

        private void Limpar()
        {
            HF_ID.Value = string.Empty;
            TXT_DESCRICAO.Text = string.Empty;
            TXT_MENU.Text = string.Empty;
            TXT_ORDEM.Text = string.Empty;
            TXT_PARAMETRO.Text = string.Empty;
            TXT_URL.Text = string.Empty;
            DDL_ID_MENU_PAI.SelectedValue = string.Empty;

        }

        protected void BTN_LIMPAR_Click(object sender, EventArgs e)
        {
            Limpar();
        }

        protected void LST_MENU_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LST_MENU.SelectedItem.Value != string.Empty)
            {

                o_tb_menu = b_tb_menu.SelecionarObjetoPorId(Convert.ToInt32(LST_MENU.SelectedItem.Value));

                TXT_DESCRICAO.Text = o_tb_menu.descricao.ToString();

                if (o_tb_menu.id_menu_pai == 0)
                {
                    DDL_ID_MENU_PAI.SelectedValue = string.Empty;
                }
                else
                {
                    DDL_ID_MENU_PAI.SelectedValue = o_tb_menu.id_menu_pai.ToString();
                }

                TXT_MENU.Text = o_tb_menu.menu.ToString();
                TXT_ORDEM.Text = o_tb_menu.ordem.ToString();
                TXT_PARAMETRO.Text = o_tb_menu.parametro.ToString();
                TXT_URL.Text = o_tb_menu.url.ToString();
                HF_ID.Value = o_tb_menu.id.ToString();
                CHK_SIT_ATIVO.Checked = o_tb_menu.sit_ativo;
            }
        }

        protected void BTN_EXCLUIR_Click(object sender, EventArgs e)
        {
            try
            {
                if (HF_ID.Value != string.Empty)
                {
                    int id_menu = Convert.ToInt32(HF_ID.Value);
                    string msg = string.Empty;

                    using (TransactionScope scope = new TransactionScope())
                    {
                        o_tb_menu_usuario.id_menu = id_menu;
                        b_tb_menu_usuario.Excluir(o_tb_menu_usuario);

                        o_tb_menu.id = id_menu;
                        b_tb_menu.Excluir(o_tb_menu);


                        msg = "Exclusão realizada com sucesso";
                        Master.MSG_ALERTA(msg, "default.aspx", false);


                        PreencheControles();
                        scope.Complete();
                        

                    }
                    
                }
            }
            catch (Exception)
            {
                
                throw;
            }
           


        }

        //protected void TXT_FILTRO_MENU_TextChanged(object sender, EventArgs e)
        //{
        //    if (TXT_FILTRO_MENU.Text != string.Empty)
        //    {
        //        LST_MENU.Items.FindByText(TXT_FILTRO_MENU.Text);

        //    }
        //    else
        //    {
        //        b_tb_menu.ListaMenu(LST_MENU, false);   
        //    }
        //}

    
    }
}